package com.topscore.uke.mvp.presenters;

import android.graphics.Color;

import com.topscore.uke.entities.PerformanceChartsEntity;
import com.topscore.uke.entities.PerformanceData;
import com.topscore.uke.entities.TeamPerformanceListEntity;
import com.topscore.uke.mvp.contractors.TeamPerformanceRankContractor;
import com.topscore.uke.mvp.model.TeamPerformanceRankModel;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.request.GetTeamSalesRequest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SubcolumnValue;

/**
 * Created by 71468 on 2017/11/28.
 */

public class TeamPerformanceRankPresenter implements TeamPerformanceRankContractor, TeamPerformanceRankContractor.Presenter {

    private View mView;
    private Model mModel;

    public TeamPerformanceRankPresenter(View view) {
        this.mView = view;
        mModel = new TeamPerformanceRankModel(mCallBack, "m1003");
    }

    private final CallBack mCallBack = new CallBack() {
        @Override
        public void onGetRankListSuccess(BaseResponse<List<TeamPerformanceListEntity>> response) {
            mView.getRankAdapter().setNewData(response.getData());
        }

        @Override
        public void onGetPerformanceDataSuccess(BaseResponse<PerformanceData> response) {
            mView.onGetPerformanceDataSuccess(String.valueOf(response.getData().getSalesMoneyTotal()), String.valueOf(response.getData().getSalesCountTotal()));
            mView.onGetPerformanceCharDataSuccess(getColumnLineData(response.getData().getSaleMoneys(), response.getData().getSaleCounts()));
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            mView.onFail(errorCode, errorMessage);
        }

        @Override
        public void onComplete() {
            mView.onLoadFinished();
        }
    };

    private ComboLineColumnChartData getColumnLineData(List<PerformanceChartsEntity> saleMoneys, List<PerformanceChartsEntity> saleCounts) {
        ComboLineColumnChartData chartData = new ComboLineColumnChartData(getColumnData(saleMoneys), getLineData(saleCounts));
        List<AxisValue> axisXValues = new ArrayList<>();
        AxisValue axisXValue;
        for (int i = 0; i < saleCounts.size(); i++) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = dateFormat.parse(saleCounts.get(i).getX());
                axisXValue = new AxisValue(i).setLabel(new SimpleDateFormat("dd").format(date));
            } catch (ParseException e) {
                e.printStackTrace();
                axisXValue = new AxisValue(i).setLabel(saleCounts.get(i).getX());
            }
            axisXValues.add(axisXValue);
        }
        Axis axisX = new Axis(axisXValues);
        axisX.setHasTiltedLabels(false);
        chartData.setAxisXBottom(axisX);
        return chartData;
    }

    private LineChartData getLineData(List<PerformanceChartsEntity> saleCounts) {
        List<Line> lines = new ArrayList<>();
        List<PointValue> pointValues = new ArrayList<>();
        for (int i = 0; i < saleCounts.size(); i++) {
            PointValue value = new PointValue(i, saleCounts.get(i).getY());
            pointValues.add(value);
        }
        Line line = new Line(pointValues);
        line.setPointRadius(3);
        line.setStrokeWidth(1);
        line.setColor(Color.parseColor("#f8b551"));
        lines.add(line);
        LineChartData lineChartData = new LineChartData(lines);
        return lineChartData;
    }

    private ColumnChartData getColumnData(List<PerformanceChartsEntity> saleMoneys) {
        List<Column> columns = new ArrayList<>();
        for (int i = 0; i < saleMoneys.size(); i++) {
            SubcolumnValue subcolumnValue = new SubcolumnValue(saleMoneys.get(i).getY());
            subcolumnValue.setColor(Color.parseColor("#CE3B3E"));
            List<SubcolumnValue> subcolumnValues = new ArrayList<>();
            subcolumnValues.add(subcolumnValue);
            Column column = new Column(subcolumnValues);
            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        return columnChartData;
    }

    @Override
    public void getData() {
        mView.onLoading("请稍候");
        mModel.getData();
    }

    @Override
    public void getRankByAmountDESC() {
        mView.onLoading("正在获取榜单数据");
        mModel.getRankByAmount(GetTeamSalesRequest._SORT_DESC);
    }

    @Override
    public void getRankByAmountASE() {
        mView.onLoading("正在获取榜单数据");
        mModel.getRankByAmount(GetTeamSalesRequest._SORT_ASC);
    }

    @Override
    public void getRankByCountDESC() {
        mView.onLoading("正在获取榜单数据");
        mModel.getRankByCount(GetTeamSalesRequest._SORT_DESC);
    }

    @Override
    public void getRankByCountASE() {
        mView.onLoading("正在获取榜单数据");
        mModel.getRankByCount(GetTeamSalesRequest._SORT_ASC);
    }

    @Override
    public void getPerformanceByYear() {
        mView.onLoading("正在获取业绩图表数据");
        mModel.getPerformanceByYear();
    }

    @Override
    public void getPerformanceByMonth() {
        mView.onLoading("正在获取业绩图表数据");
        mModel.getPerformanceByMonth();
    }

    @Override
    public void getPerformanceByYear(String year) {
        mView.onLoading("正在获取业绩图表数据");
        mModel.getPerformanceByYear(year);
    }

    @Override
    public void getPerformanceByMonth(String month) {
        mView.onLoading("正在获取业绩图表数据");
        mModel.getPerformanceByMonth(month);
    }
}
